package com.enjia.shopping.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.enjia.common.framework.BaseFragment;
import com.enjia.common.utils.LogUtils;
import com.enjia.common.utils.ToastUtil;
import com.enjia.enjiashop.R;
import com.enjia.shopping.adapter.ShopCartAdapter;
import com.enjia.shopping.bean.ShopingBean;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import butterknife.BindView;

/**
 * 购物车Fragment
 * Created by feifei on 2017/12/5.
 */

public class ShoppingFragment extends BaseFragment implements View.OnClickListener{

    private View root_view;
    //商品列表
    @BindView(R.id.recycle_shopList)
    EasyRecyclerView recycle_shopList;
    //已选商品按钮
    @BindView(R.id.shop_checkAll)
    CheckBox shop_checkAll;
    //编辑按钮
    @BindView(R.id.shop_edit)
    TextView shop_edit;
    //选择删除布局
    @BindView(R.id.layout_selectAll)
    RelativeLayout layout_selectAll;
    //底部结算布局
    @BindView(R.id.layout_editClearing)
    RelativeLayout layout_editClearing;
    //删除按钮
    @BindView(R.id.btn_deleteAll)
    TextView btn_deleteAll;
    //结算按钮
    @BindView(R.id.btn_shop_clearing)
    TextView btn_shop_clearing;

    /******************************************    变量适配    ********************************************/
    //记录当前页面状态是编辑还是选择删除
    private boolean isSelectState = false;
    //商品列表适配器
    private ShopCartAdapter shopCartAdapter;
    //总体商品列表数据
    private List<ShopingBean> shopingList;

    @Override
    protected View onCreateView(LayoutInflater mInflater) {
        root_view = mInflater.inflate(R.layout.fragment_shopping,null);
        return root_view;
    }

    @Override
    protected boolean hasTitleHeader() {
        return false;
    }

    @Override
    public void initView() {
        recycle_shopList.setLayoutManager(new LinearLayoutManager(mContext));
        shopCartAdapter = new ShopCartAdapter(mContext);
        recycle_shopList.setAdapter(shopCartAdapter);
        shopingList = getShopIngList();
        shopCartAdapter.addAll(shopingList);
    }

    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
        shop_edit.setOnClickListener(this);
        btn_deleteAll.setOnClickListener(this);
        btn_shop_clearing.setOnClickListener(this);
        //商品选择CheckBox
        shopCartAdapter.setOnSelectItemListener(new ShopCartAdapter.OnSelectItemListener() {
            @Override
            public void checkSelectClick(int position, boolean isSelect) {
                if(isSelect){
                    shopingList.get(position).setSelect(true);
                    ToastUtil.showMessage(mContext,"添加了=="+position);
                }else{
                    shopingList.get(position).setSelect(false);
                    ToastUtil.showMessage(mContext,"删除了=="+position);
                    if(shop_checkAll.isChecked()){
                        shop_checkAll.setChecked(false);
                    }
                }
            }
        });
        //全选商品按钮
        shop_checkAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(shop_checkAll.isChecked()){
                   for(int i = 0;i<shopingList.size();i++){
                       shopingList.get(i).setSelect(true);
                   }
                }
                shopCartAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //编辑按钮
            case R.id.shop_edit:
                if(isSelectState){
                    layout_selectAll.setVisibility(View.GONE);
                    layout_editClearing.setVisibility(View.VISIBLE);
                    shop_edit.setText("编辑");
                    isSelectState = false;
                }else{
                    layout_selectAll.setVisibility(View.VISIBLE);
                    layout_editClearing.setVisibility(View.GONE);
                    shop_edit.setText("完成");
                    isSelectState = true;
                }
                for(int i = 0;i<shopingList.size();i++){
                    shopingList.get(i).setSelect(false);
                }
                shopCartAdapter.notifyDataSetChanged();
                break;
            //删除按钮
            case R.id.btn_deleteAll:
                for(int i = shopingList.size()-1;i>=0;i--){
                    if(shopingList.get(i).isSelect()){
                        shopingList.remove(i);
                        shopCartAdapter.remove(i);
                    }
                }
                shopCartAdapter.notifyDataSetChanged();
                break;
            //计算按钮
            case R.id.btn_shop_clearing:
                for(int i = 0;i<shopingList.size();i++){
                    if(shopingList.get(i).isSelect()){
                        LogUtils.e("结算的商品ID=="+shopingList.get(i).getShopID());
                    }
                }
                jumpToFragment(new ShopOrderFragment());
                break;
        }
    }

    @Override
    protected String getTitleValue() {
        return null;
    }
    //模拟购物车集合
    private List<ShopingBean> getShopIngList(){
        List<ShopingBean> shopList = new ArrayList<>();
        for(int i = 0;i<10;i++){
            ShopingBean shopBean = new ShopingBean();
            shopBean.setShopID(i);
            shopList.add(shopBean);
        }
        return  shopList;
    }
}
